home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / python2.6 / abc.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2013-01-10  |  5.9 KB  |  177 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Abstract Base Classes (ABCs) according to PEP 3119.'''
  5.  
  6. class _C:
  7.     pass
  8.  
  9. _InstanceType = type(_C())
  10.  
  11. def abstractmethod(funcobj):
  12.     """A decorator indicating abstract methods.
  13.  
  14.     Requires that the metaclass is ABCMeta or derived from it.  A
  15.     class that has a metaclass derived from ABCMeta cannot be
  16.     instantiated unless all of its abstract methods are overridden.
  17.     The abstract methods can be called using any of the normal
  18.     'super' call mechanisms.
  19.  
  20.     Usage:
  21.  
  22.         class C:
  23.             __metaclass__ = ABCMeta
  24.             @abstractmethod
  25.             def my_abstract_method(self, ...):
  26.                 ...
  27.     """
  28.     funcobj.__isabstractmethod__ = True
  29.     return funcobj
  30.  
  31.  
  32. class abstractproperty(property):
  33.     """A decorator indicating abstract properties.
  34.  
  35.     Requires that the metaclass is ABCMeta or derived from it.  A
  36.     class that has a metaclass derived from ABCMeta cannot be
  37.     instantiated unless all of its abstract properties are overridden.
  38.     The abstract properties can be called using any of the normal
  39.     'super' call mechanisms.
  40.  
  41.     Usage:
  42.  
  43.         class C:
  44.             __metaclass__ = ABCMeta
  45.             @abstractproperty
  46.             def my_abstract_property(self):
  47.                 ...
  48.  
  49.     This defines a read-only property; you can also define a read-write
  50.     abstract property using the 'long' form of property declaration:
  51.  
  52.         class C:
  53.             __metaclass__ = ABCMeta
  54.             def getx(self): ...
  55.             def setx(self, value): ...
  56.             x = abstractproperty(getx, setx)
  57.     """
  58.     __isabstractmethod__ = True
  59.  
  60.  
  61. class ABCMeta(type):
  62.     """Metaclass for defining Abstract Base Classes (ABCs).
  63.  
  64.     Use this metaclass to create an ABC.  An ABC can be subclassed
  65.     directly, and then acts as a mix-in class.  You can also register
  66.     unrelated concrete classes (even built-in classes) and unrelated
  67.     ABCs as 'virtual subclasses' -- these and their descendants will
  68.     be considered subclasses of the registering ABC by the built-in
  69.     issubclass() function, but the registering ABC won't show up in
  70.     their MRO (Method Resolution Order) nor will method
  71.     implementations defined by the registering ABC be callable (not
  72.     even via super()).
  73.  
  74.     """
  75.     _abc_invalidation_counter = 0
  76.     
  77.     def __new__(mcls, name, bases, namespace):
  78.         cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace)
  79.         abstracts = set((lambda .0: for name, value in .0:
  80. if getattr(value, '__isabstractmethod__', False):
  81. namecontinue)(namespace.items()))
  82.         for base in bases:
  83.             for name in getattr(base, '__abstractmethods__', set()):
  84.                 value = getattr(cls, name, None)
  85.                 if getattr(value, '__isabstractmethod__', False):
  86.                     abstracts.add(name)
  87.                     continue
  88.             
  89.         
  90.         cls.__abstractmethods__ = frozenset(abstracts)
  91.         cls._abc_registry = set()
  92.         cls._abc_cache = set()
  93.         cls._abc_negative_cache = set()
  94.         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
  95.         return cls
  96.  
  97.     
  98.     def register(cls, subclass):
  99.         '''Register a virtual subclass of an ABC.'''
  100.         if not isinstance(cls, type):
  101.             raise TypeError('Can only register classes')
  102.         isinstance(cls, type)
  103.         if issubclass(subclass, cls):
  104.             return None
  105.         if issubclass(cls, subclass):
  106.             raise RuntimeError('Refusing to create an inheritance cycle')
  107.         issubclass(cls, subclass)
  108.         cls._abc_registry.add(subclass)
  109.         ABCMeta._abc_invalidation_counter += 1
  110.  
  111.     
  112.     def _dump_registry(cls, file = None):
  113.         '''Debug helper to print the ABC registry.'''
  114.         print >>file, 'Class: %s.%s' % (cls.__module__, cls.__name__)
  115.         print >>file, 'Inv.counter: %s' % ABCMeta._abc_invalidation_counter
  116.         for name in sorted(cls.__dict__.keys()):
  117.             if name.startswith('_abc_'):
  118.                 value = getattr(cls, name)
  119.                 print >>file, '%s: %r' % (name, value)
  120.                 continue
  121.         
  122.  
  123.     
  124.     def __instancecheck__(cls, instance):
  125.         '''Override for isinstance(instance, cls).'''
  126.         subclass = getattr(instance, '__class__', None)
  127.         if subclass in cls._abc_cache:
  128.             return True
  129.         subtype = type(instance)
  130.         if subtype is _InstanceType:
  131.             subtype = subclass
  132.         
  133.         if subtype is subclass or subclass is None:
  134.             if cls._abc_negative_cache_version == ABCMeta._abc_invalidation_counter and subtype in cls._abc_negative_cache:
  135.                 return False
  136.             return cls.__subclasscheck__(subtype)
  137.         if not cls.__subclasscheck__(subclass):
  138.             pass
  139.         return cls.__subclasscheck__(subtype)
  140.  
  141.     
  142.     def __subclasscheck__(cls, subclass):
  143.         '''Override for issubclass(subclass, cls).'''
  144.         if subclass in cls._abc_cache:
  145.             return True
  146.         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
  147.             cls._abc_negative_cache = set()
  148.             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
  149.         elif subclass in cls._abc_negative_cache:
  150.             return False
  151.         ok = cls.__subclasshook__(subclass)
  152.         if ok is not NotImplemented:
  153.             if not isinstance(ok, bool):
  154.                 raise AssertionError
  155.             if ok:
  156.                 cls._abc_cache.add(subclass)
  157.             else:
  158.                 cls._abc_negative_cache.add(subclass)
  159.             return ok
  160.         if cls in getattr(subclass, '__mro__', ()):
  161.             cls._abc_cache.add(subclass)
  162.             return True
  163.         for rcls in cls._abc_registry:
  164.             if issubclass(subclass, rcls):
  165.                 cls._abc_cache.add(subclass)
  166.                 return True
  167.         
  168.         for scls in cls.__subclasses__():
  169.             if issubclass(subclass, scls):
  170.                 cls._abc_cache.add(subclass)
  171.                 return True
  172.         
  173.         cls._abc_negative_cache.add(subclass)
  174.         return False
  175.  
  176.  
  177.